package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

/**
 * 商品业务实现类
 */
public class ProductServiceImpl extends BaseService implements IProductService {
    @Override
    public Integer add(ProductBean product) {
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.add(product);
        session.commit();
        session.close();
        return product.getId();
    }

    @Override
    public void update(Integer id, Integer price) {
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.update(id,price);
        session.commit();
        session.close();
    }

    @Override
    public ProductBean findById(Integer id) {
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        ProductBean product = mapper.findById(id);
        session.close();
        return product;
    }

    @Override
    public CutPageBean<ProductBean> cutByItem(int pageNO,String name, Integer minPrice, Integer maxPrice) {
        CutPageBean<ProductBean> cutPage = new CutPageBean<>();
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        cutPage.setList(mapper.cutByItemListO((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE,name,minPrice,maxPrice));
        cutPage.setCount(mapper.cutByItemCountO(name,minPrice,maxPrice));
        session.close();
        return cutPage;
    }

    @Override
    public CutPageBean<ProductBean> cutByItem(int pageNO,String factory, LocalDate startDate, LocalDate endDate) {
        CutPageBean<ProductBean> cutPage = new CutPageBean<>();
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        cutPage.setList(mapper.cutByItemListT((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE,factory,startDate,endDate));
        cutPage.setCount(mapper.cutByItemCountT(factory,startDate,endDate));
        session.close();
        return cutPage;
    }

    @Override
    public void addList(List<ProductBean> list) {
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.addList(list);
        session.commit();
        session.close();
    }

    @Override
    public void delArray(Integer[] idArray) {
        //获取会话对象
        SqlSession session = this.getSession();
        //获取mapper对象
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.delArray(idArray);
        session.commit();
        session.close();
    }
}
